package edu.ustb.controller;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.alibaba.fastjson.JSON;

import edu.ustb.domain.Message;
import edu.ustb.domain.User;
import edu.ustb.mapper.UserMapper;
import edu.ustb.result.Result;


@WebServlet("/user/register.do")
@MultipartConfig
public class UserRegisterController extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=utf-8");
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		String userCode = request.getParameter("userCode");
		String passwordagain = request.getParameter("passwordagain");
		String email = request.getParameter("email");
		
		System.out.println(phone+","+password+","+userCode+","+passwordagain+","+email);
		Result result = new Result();
		Message message = new Message();
		message.setUserAddress(email);
		message.setUserPhone(phone);
		message.setUserPass(password);
		
		
		try {
			
			InputStream	is = Resources.getResourceAsStream("mybatis.xml");
			SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
			SqlSession openSession = ssf.openSession(true);
			UserMapper mapper = openSession.getMapper(UserMapper.class);
				//UserMapper mapper = MybatisUtil.getMapper(UserMapper.class);
				//System.out.println(mapper);
				//Users user = mapper.findByUserName(phone);
			User user = new User();
				int rows = mapper.save(message);
				//System.out.println(user);
				
		
		 
		        //用户输入的验证码

		        //VerifyCodeImg存在session中的验证码
		        String rightCode=null;
		        Object code = request.getSession().getAttribute("code");
		        if(code!=null){
		            rightCode=(String) code;
		        }


		        //用户输入和生成验证码的比较，并生成不同的信息提示
		        
		        //用户输入和生成验证码的比较，并生成不同的信息提示
		        if(userCode.equalsIgnoreCase(rightCode)){
		        	
		        	
		        	
		        	if(password.equals(passwordagain) ) {
		        		
		    			result.setNode(5);
		    			result.setMsg("注册成功");
		    			response.getWriter().print(JSON.toJSONString(result));
		    			System.out.println(JSON.toJSONString(result));
		    			
		    		}else {
		    			result.setNode(2);
		    			result.setMsg("两次密码错误");
		    			response.getWriter().print(JSON.toJSONString(result));
		    			System.out.println(JSON.toJSONString(result));
		    	}
		         
		        }else{//System.out.println("55555555555555555555555555555555555555555555555555555555");
		        	result.setNode(0);
					result.setMsg("验证码错误");
					
					
					response.getWriter().print(JSON.toJSONString(result));
					System.out.println(JSON.toJSONString(result));
		        }
		

	
		

			//user.getUser_pass().equals(phone)
			
			} catch (IOException e) {
				
			e.printStackTrace();
			}
			

	}
		
		
	}


